//
//  LandmarkDetail.swift
//  Landmarks
//
//  Created by 温哲 on 2021/2/10.
//

import SwiftUI

struct LandmarkDetail: View {
    @EnvironmentObject var modelData: ModelData
    
    var landmark: Landmark
    
    var landmarkIndex: Int {
        modelData.landmarks.firstIndex(where: { $0.id == landmark.id })!
    }

    var body: some View {
        ScrollView {
            MapView(coordinate: landmark.locationCoordinate).ignoresSafeArea(.keyboard, edges: .top).frame(height: 300, alignment: /*@START_MENU_TOKEN@*/ .center/*@END_MENU_TOKEN@*/)
            CircleImage(image: landmark.image).offset(x: 0, y: -130).padding(EdgeInsets(top: 10, leading: 10, bottom: -130, trailing: 10))
            VStack(alignment: .leading) {
                HStack {
                    Text(landmark.name)
                        .font(.title)
                    FavoriteButton(isSet: $modelData.landmarks[landmarkIndex].isFavorite)
                }
                HStack {
                    Text(landmark.park)
                        .font(.subheadline)
                    Spacer()
                    Text(landmark.state)
                        .font(.subheadline)
                }.font(.subheadline).foregroundColor(.secondary)

                Divider()
                Text("About \(landmark.name)").font(.title2)
                Text(landmark.description)
            }.padding(.horizontal, 50.0)

        }.navigationTitle(landmark.name).navigationBarTitleDisplayMode(.inline)
    }
}

struct LandmarkDetail_Previews: PreviewProvider {
    static let modelData = ModelData()
    
    static var previews: some View {
        LandmarkDetail(landmark: ModelData().landmarks[0]).environmentObject(modelData)
    }
}
